package com.TeacherInfoServer.common.service.Impl;

import com.TeacherInfoServer.pojo.DTO.AddTypeInfoDTO;
import com.TeacherInfoServer.pojo.entity.TypeInfo;
import com.TeacherInfoServer.mapper.TypeInfoMapper;
import com.TeacherInfoServer.common.service.TypeInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class TypeInfoServiceImpl extends ServiceImpl<TypeInfoMapper, TypeInfo> implements TypeInfoService {
    @Autowired
    public TypeInfoMapper typeInfoMapper;

    @Override
    public Map<String, Object> getListByName(Integer pages, Integer size, String name){
        Page<TypeInfo> page = new Page<>(pages, size);
        QueryWrapper<TypeInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        typeInfoMapper.selectPage(page,queryWrapper);
        Map<String, Object> data = new HashMap<>();
        data.put("totals",page.getTotal());
        data.put("currentPages",page.getCurrent());
        data.put("pageNumber",page.getPages());
        data.put("rows",page.getRecords());
        return data;
    }
    @Override
    public Map<String, Object> getList(Integer pages, Integer size){
        Page<TypeInfo> page = new Page<>(pages, size);
        QueryWrapper<TypeInfo> queryWrapper = new QueryWrapper<>();
        typeInfoMapper.selectPage(page,queryWrapper);
        Map<String, Object> data = new HashMap<>();
        data.put("totals",page.getTotal());
        data.put("currentPages",page.getCurrent());
        data.put("pageNumber",page.getPages());
        data.put("rows",page.getRecords());
        return data;
    }

    @Override
    public List<TypeInfo> getOneNameList(String name){
        QueryWrapper<TypeInfo> queryWrapper =new QueryWrapper<>();
        queryWrapper.select("value","id")
                .eq("name",name);
        List<TypeInfo> typeInfoList=typeInfoMapper.selectList(queryWrapper);
        return typeInfoList;
    }
    @Override
    public TypeInfo getOne(String name,String value){
        QueryWrapper<TypeInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name).eq("value",value);
        TypeInfo typeInfo =typeInfoMapper.selectOne(queryWrapper);
        return typeInfo;
    }
    @Override
    public Integer getOneId(String name,String value){
        QueryWrapper<TypeInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name).eq("value",value);
        TypeInfo typeInfo =typeInfoMapper.selectOne(queryWrapper);
        if(typeInfo !=null) {
            return typeInfo.getId();
        }
        else {
            return null;
        }
    }
    @Override
    public String getValue(Integer id){
        TypeInfo typeInfo =typeInfoMapper.selectById(id);
        return typeInfo.getValue();
    }

    @Override
    public String addOne(AddTypeInfoDTO addTypeInfoDTO) {

        QueryWrapper<TypeInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",addTypeInfoDTO.getName()).eq("value",addTypeInfoDTO.getValue());
        boolean exists = typeInfoMapper.exists(queryWrapper);
        if(exists){
            return "添加失败，已经存在该类型";
        }

        TypeInfo typeInfo = new TypeInfo();
        typeInfo.setName(addTypeInfoDTO.getName());
        typeInfo.setValue(addTypeInfoDTO.getValue());
        typeInfo.setCategory(addTypeInfoDTO.getCategory());
        typeInfoMapper.insert(typeInfo);
        return "添加成功";
    }

    @Override
    public List<TypeInfo> getByCategoryList(String category){
        QueryWrapper<TypeInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category);
        List<TypeInfo> typeInfoList = typeInfoMapper.selectList(queryWrapper);

        // 排序，首先按 name 排序，然后在 name 相同的情况下按 id 排序
        List<TypeInfo> sortedList = typeInfoList.stream()
                .sorted(Comparator.comparing(TypeInfo::getName)
                        .thenComparing(TypeInfo::getId))
                .collect(Collectors.toList());

        for (int i = 0; i < sortedList.size(); i++) {
            System.out.println(sortedList.get(i).toString());
        }

        return sortedList;
    }

}
